// 精灵的基类，负责初始化精灵加载的资源和大小以及位置
import {DataStore} from "./dataStore.js";

export class Sprite {
  constructor(
              img = null, //图片资源
              srcX = 0, //源图像的矩形选择框的左上角 x 坐标
              srcY = 0,
              srcW = 0,//源图像的矩形选择框的宽度
              srcH = 0,
              x = 0, //图像的左上角在目标 canvas 上 x 轴的位置
              y = 0,
              width = 0, // 目标画布上绘制图像的宽度，允许对绘制的图像进行缩放
              height = 0) {
    this.dataStore = DataStore.getInstance();
    this.ctx = this.dataStore.ctx;
    this.img = img;
    this.srcX = srcX;
    this.srcY = srcY;
    this.srcW = srcW;
    this.srcH = srcH;
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
  }
  static getImage(key){
    // console.log(DataStore.instance) // 在main.js 中其实已经创建过该单例了
    return DataStore.getInstance().res.get(key)
  }
  /**
   * img 传入Image对象
   * srcX 要剪裁的起始X坐标
   * srcY 要剪裁的起始Y坐标
   * srcW 剪裁的宽度
   * srcH 剪裁的高度
   * x 放置的x坐标
   * y 放置的y坐标
   * width 要使用的宽度
   * height 要使用的高度
   */
  draw(img = this.img,
    srcX = this.srcX,
    srcY = this.srcY,
    srcW = this.srcW,
    srcH = this.srcH,
    x = this.x,
    y = this.y,
    width = this.width,
    height = this.height) {
    this.ctx.drawImage(
      img,
      srcX,
      srcY,
      srcW,
      srcH,
      x,
      y,
      width,
      height
    );
  }
}